package br.com.dev.tcc.view.sinistro.chain.incendio;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.singleton.SinistroSingleton;
import br.com.dev.tcc.util.EnderecoUtil;
import br.com.dev.tcc.view.dialog.DatePickerDialog;
import br.com.dev.tcc.view.dialog.DatePickerDialog.OnDatePickerListener;
import br.com.dev.tcc.view.sinistro.chain.SinistroChainItem;

public class SinistroIncendioPasso1 extends SinistroChainItem {

	private View txtData;
	private EditText valueData;
	private EditText valueDescritivo;
	private EditText valueLocal;
	private View imgGps;
	private DatePickerDialog dateDialog;

	public SinistroIncendioPasso1(Context context) {
		super(context);
		inflateView(R.layout.sinistro_tipo_incendio1);
	}

	@Override
	public void init() {

		dateDialog = new DatePickerDialog(context, new OnDatePickerListener() {
			
			@Override
			public void onDateSelected(String date) {
				valueData.setText(date);
			}
		});
		
		OnClickListener showDataPicker = new OnClickListener() {
			@Override
			public void onClick(View v) {
				dateDialog.show();
			}
		};
		
		txtData = view.findViewById(R.id.txtData);
		txtData.setOnClickListener(showDataPicker);
		valueData = (EditText) view.findViewById(R.id.txtValorData);
		valueData.setOnClickListener(showDataPicker);

		imgGps = view.findViewById(R.id.ivGps);
		imgGps.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new EnderecoUtil().searchEndereco(context, valueLocal);
			}
		});
		valueLocal = (EditText) view.findViewById(R.id.txtValorEndereco);

		valueDescritivo = (EditText) view.findViewById(R.id.txtValorDescritivo);
		valueDescritivo.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_UP
						&& keyCode == KeyEvent.KEYCODE_ENTER) {
					InputMethodManager imm = (InputMethodManager) context
							.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(
							valueDescritivo.getWindowToken(), 0);
				}
				return true;
			}
		});
	}

	@Override
	public boolean isValid() {

		boolean isValid = true;

		if (valueDescritivo.getText().toString().trim().length() == 0) {

			valueDescritivo.setCompoundDrawablesWithIntrinsicBounds(null, null,
					context.getResources().getDrawable(R.drawable.exclamation),
					null);
			valueDescritivo.requestLayout();
			valueDescritivo.requestFocus();
			isValid = false;
		} else
			valueDescritivo.setCompoundDrawablesWithIntrinsicBounds(null, null,
					null, null);

		if (valueLocal.getText().toString().trim().length() == 0) {

			valueLocal.setCompoundDrawablesWithIntrinsicBounds(null, null,
					context.getResources().getDrawable(R.drawable.exclamation),
					null);
			valueLocal.requestLayout();
			valueLocal.requestFocus();
			isValid = false;
		} else
			valueLocal.setCompoundDrawablesWithIntrinsicBounds(null, null,
					null, null);

		if (valueData.getText().toString().trim().length() == 0) {

			valueData.setCompoundDrawablesWithIntrinsicBounds(null, null,
					context.getResources().getDrawable(R.drawable.exclamation),
					null);
			valueData.requestLayout();
			valueData.requestFocus();
			isValid = false;
		} else
			valueData.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
					null);

		return isValid;
	}

	@Override
	public void setSinistroFields() {

		Sinistro sinistro = SinistroSingleton.getInstance();
		sinistro.setData(valueData.getText().toString());
		sinistro.setDescritivo(valueDescritivo.getText().toString());
		sinistro.setLocal(valueLocal.getText().toString());
	}

	@Override
	public View getFirstView() {
		return txtData;
	}

	@Override
	public void onShow() {
		txtData.requestFocus();
	}
}
